import {RouteRecordRaw} from "vue-router";
import {constantRoutes} from "@/router";
import {store, useUserStore} from "@/store";
import MenuAPI from "@/api/menu";
import {RouteVO} from "@/api/menu/model";
import {MenuTypeEnum} from "@/enums/MenuTypeEnum";

var userStore: any;
const modules = import.meta.glob("../../views/**/**.vue");
const Layout = () => import("@/layout/index.vue");

/**
 * Use meta.role to determine if the current user has permission
 *
 * @param roles 用户角色集合
 * @param route 路由
 * @returns
 */
const hasPermission = (roles: string[], route: RouteRecordRaw) => {
    if (route.meta && route.meta.roles) {
        // 角色【超级管理员】拥有所有权限，忽略校验
        if (roles.includes("ROOT")) {
            return true;
        }
        return roles.some((role) => {
            if (route.meta?.roles) {
                return route.meta.roles.includes(role);
            }
        });
    }
    return false;
};

/**
 * 递归过滤有权限的动态路由
 *
 * @param routes 接口返回所有的动态路由
 * @param roles 用户角色集合  routes: RouteVO[]
 * @returns 返回用户有权限的动态路由
 */
const filterAsyncRoutes = (menus: string[]) => {
    const asyncRoutes: RouteRecordRaw[] = [];
    menus.forEach((route: any) => {
        const tmpRoute = JSON.parse(JSON.stringify(route)); // 深拷贝 route 对象 避免污染
        // if (hasPermission(roles, tmpRoute)) {
        // 如果是顶级目录，替换为 Layout 组件
        if (route.perm) {
            userStore.user.perms.push(route.perm)
        }
        if (tmpRoute.type != MenuTypeEnum.BUTTON) {
            if (tmpRoute.component?.toString() == "Layout") {
                tmpRoute.component = Layout;
            } else {
                // 如果是子目录，动态加载组件
                const component = modules[`../../views/${tmpRoute.component}.vue`];
                if (component) {
                    tmpRoute.component = component;
                } else {
                    tmpRoute.component = modules[`../../views/error-page/404.vue`];
                }
            }
            tmpRoute.meta = {}
            // tmpRoute.meta.alwaysShow = (!(route.alwaysShow && route.alwaysShow == 0))
            tmpRoute.meta.alwaysShow = false
            tmpRoute.meta.keepAlive = route.keepAlive == 1
            tmpRoute.meta.hidden = route.visible == 0
            tmpRoute.meta.icon = route.icon
            tmpRoute.meta.params = route.params
            tmpRoute.meta.title = route.name
            //name必须唯一 https://router.vuejs.org/guide/essentials/named-routes.html
            if(route.perm){
                tmpRoute.name = route.perm
            }else{
                tmpRoute.name = route.name + route.id
            }

            if (tmpRoute.children) {
                tmpRoute.children = filterAsyncRoutes(route.children);
            }

            asyncRoutes.push(tmpRoute);
        }
    });

    return asyncRoutes;
};
// setup
export const usePermissionStore = defineStore("permission", () => {
    // state
    const routes = ref<RouteRecordRaw[]>([]);

    // actions
    function setRoutes(newRoutes: RouteRecordRaw[]) {
        routes.value = constantRoutes.concat(newRoutes);
    }

    /**
     * 生成动态路由
     *
     * @param roles 用户角色集合
     * @returns
     */
    function generateRoutes(menus: string[]) {
        userStore = useUserStore();
        userStore.user.perms = []
        const accessedRoutes = filterAsyncRoutes(menus);
        setRoutes(accessedRoutes);
        return accessedRoutes;
        // return new Promise<RouteRecordRaw[]>((resolve, reject) => {
        //   // 接口获取所有路由
        //   MenuAPI.getRoutes()
        //     .then((data) => {
        //       // 过滤有权限的动态路由
        //
        //       resolve(accessedRoutes);
        //     })
        //     .catch((error) => {
        //       reject(error);
        //     });
        // });
    }

    /**
     * 获取与激活的顶部菜单项相关的混合模式左侧菜单集合
     */
    const mixLeftMenus = ref<RouteRecordRaw[]>([]);

    function setMixLeftMenus(topMenuPath: string) {
        const matchedItem = routes.value.find((item) => item.path === topMenuPath);
        if (matchedItem && matchedItem.children) {
            mixLeftMenus.value = matchedItem.children;
        }
    }

    return {
        routes,
        setRoutes,
        generateRoutes,
        mixLeftMenus,
        setMixLeftMenus,
    };
});

// 非setup
export function usePermissionStoreHook() {
    return usePermissionStore(store);
}
